package mini_program

import (
	"context"
	pb "git.mycaigou.com/gfyx/micro-gfyx-user-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
	miniProgramService "git.mycaigou.com/gfyx/micro-gfyx-user-service/service/mini_program"
	"git.myscrm.cn/golang/stark/v4"
)

type miniProgramServer struct {
	miniProgramService miniProgramService.MiniProgramServiceIface
}

func NewMiniProgramServer(
	miniProgramService miniProgramService.MiniProgramServiceIface,
) pb.MiniProgramServiceServer {
	return &miniProgramServer{
		miniProgramService: miniProgramService,
	}
}

func (s *miniProgramServer) GetAccessToken(ctx context.Context, request *pb.GetAccessTokenRequest) (*pb.GetAccessTokenResponse, error) {
	resp, err := s.miniProgramService.GetAccessToken(ctx)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetAccessToken err:%s", err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *miniProgramServer) GetQRCode(ctx context.Context, request *pb.GetQRCodeRequest) (*pb.GetQRCodeResponse, error) {
	resp, err := s.miniProgramService.GetQRCode(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetQRCode err:%s", err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *miniProgramServer) ClearAccessTokenCache(ctx context.Context, request *pb.ClearAccessTokenCacheRequest) (*pb.ClearAccessTokenCacheResponse, error) {
	err := s.miniProgramService.ClearAccessTokenCache(ctx)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ClearAccessTokenCache err:%s", err.Error())
		return nil, err
	}
	return &pb.ClearAccessTokenCacheResponse{IsSuccess: true}, nil
}

func (s *miniProgramServer) GetMiniProgramOpenId(ctx context.Context, request *pb.GetMiniProgramOpenIdRequest) (*pb.GetMiniProgramOpenIdResponse, error) {
	resp, err := s.miniProgramService.GetMiniProgramOpenId(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetMiniProgramOpenId err:%s", err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *miniProgramServer) GetMobile(ctx context.Context, request *pb.GetMobileRequest) (*pb.GetMobileResponse, error) {
	mobile, err := s.miniProgramService.GetMobile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetMobile request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	resp := &pb.GetMobileResponse{Mobile: mobile}
	return resp, nil
}

func (s *miniProgramServer) GetQrCodeInfoById(ctx context.Context, request *pb.GetQrCodeInfoByIdRequest) (*pb.GetQrCodeInfoByIdResponse, error) {
	resp, err := s.miniProgramService.GetQrCodeInfoById(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetQrCodeInfoById request:%#v, err:%s", request, err.Error())
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc GetQrCodeInfoById request:%#v,resp:%+v", request, resp)
	return resp, nil
}
